 /**
 * 
 */
package com.clh.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.clh.common.PageInfo;
import com.clh.dao.RepairDao;
import com.clh.dao.impl.RepairDaoImpl;
import com.clh.entity.Pay;
import com.clh.entity.PayType;
import com.clh.entity.Repair;
import com.clh.service.RepairService;

/**
  * @Description: TODO
  * @author zyw
  * @date 2022-5-25 21:50:23
  * @version v1.0
 */
public class RepairServiceImpl implements RepairService{
	private RepairDao repairDao = new RepairDaoImpl();

	
	//报修列表
	@Override
	public PageInfo<Repair> showRepairList(Repair repair, int pageNum, int pageSize) {
		StringBuffer repairNoBuffer = new StringBuffer();
		
		if(repair.getRepairNO() != null && !"".equals(repair.getRepairNO())) {
			
			repairNoBuffer.append(repair.getRepairNO()).append("%");
			repair.setRepairNO(repairNoBuffer.toString());
		}
		
		//报修列表
		List<Repair> list = repairDao.findRepairList(repair,pageNum,pageSize);
		//报修列表总条数
		int total = repairDao.countByCondition(repair.getRepairCreateTime(),repair.getRepairDealTime(),repair.getRepairNO());
		
		PageInfo<Repair> pageInfo = new PageInfo<Repair>(list, pageNum, pageSize, total);
		return pageInfo;
	}


	@Override
	public Repair findByIdRepairInformation(String repairNo) {
		
		return repairDao.queryByIdRepairInformation(repairNo);
	}


	@Override
	public boolean updateRepairInformation(String repairHandler, String repairStatus, String repairNo, String repairDealTime) {
		if("02".equals(repairStatus)) {
			if("暂无时间".equals(repairDealTime)) {
				//当前时间
				LocalDateTime nowDate = LocalDateTime.now();
				DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
				repairDealTime = nowDate.format(formatter);
				
			}else {
				repairDealTime = "";
			}
		}else {
			repairHandler = null;
			repairDealTime = null;
		}
		
		return repairDao.updateRepair(repairHandler, repairStatus, repairNo,repairDealTime) > 0;
	}


	@Override
	public boolean deleteRepairInformation(String repairNo) {
		
		return repairDao.deleteReapir(repairNo) > 0;
	}


	//单个用户的报修记录
	@Override
	public PageInfo<Repair> ownerShowRepairList(String phoneNumber, String repairContent, int pageNum, int pageSize) {
		
		if(repairContent != null && !"".equals(repairContent)) {
			repairContent = new StringBuffer().append("%").append(repairContent).append("%").toString();
		}
		List<Repair> list = repairDao.ownerRepairList(phoneNumber, repairContent, pageNum, pageSize);
		int total = repairDao.ownerCountByCondition(phoneNumber, repairContent);
		PageInfo<Repair> pageInfo = new PageInfo<Repair>(list, pageNum, pageSize, total);
		return pageInfo;
	}


	@Override
	public boolean ownerRepairStatusUpdate(String repairNo, String repairStatus) {
		
		return repairDao.wonerRepairStatusUpdate(repairNo, repairStatus) > 0;
	}


	@Override
	public boolean ownerRepairInsert(Repair repair) {
		
		
		
		
		//报修编号
		UUID uuid = UUID.randomUUID();
		
		repair.setRepairNO(uuid.toString().replace("-", ""));
		System.out.println("repairService"+uuid.toString().replace("-", ""));
		//当前时间
		LocalDateTime nowDate = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		repair.setRepairCreateTime(nowDate.format(formatter));
		
		if(repair.getRepairStatus() == null) {
			repair.setRepairStatus("01");
		}

		
		return repairDao.ownerRepairAdd(repair) > 0;
	}


	//用户缴费单号生成
	@Override
	public boolean ownerRepairPay(Pay pay, String repairNo) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		if(pay.getPayStartTime() == null || "".equals(pay.getPayStartTime())) {
			//当前时间
			LocalDateTime nowDate = LocalDateTime.now();
			pay.setPayStartTime(nowDate.format(formatter));
		}
		if(pay.getPayEndTime() == null || "".equals(pay.getPayEndTime())) {
			//获取当前毫秒数
			LocalDateTime now = LocalDateTime.now();
			LocalDateTime plusTime = now.plusMonths(1);
			pay.setPayEndTime(plusTime.toString());
		}
		
		//生成缴费单号
		if(pay.getPayNo() == null || "".equals(pay.getPayNo())) {
			Long payNo = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
			int sj = (int)(Math.random()*(9999-1000+1))+1000;
			String payNoStr = payNo+sj+""+sj;
			pay.setPayNo(payNoStr);
		}
		PayType payType = new PayType();
		payType.setTypeId(3);
		pay.setPayType(payType);
		pay.setPayStatus(0);
		int n1 = repairDao.ownerReapirPay(pay);
		int n2 = repairDao.ownerRepairPayNoAdd(repairNo, pay.getPayNo());
		
		
		return n1 == n2 ;
	}

}
